package com.runa.monitor.platinterface.backwork.impl;

import com.alibaba.fastjson.JSONObject;
import com.runa.monitor.platinterface.backwork.function.AbstractTaskFunction;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.base.BaseContainer;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.equdata.*;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

@Slf4j
public class SelfTaskFunction extends AbstractTaskFunction<String, Object, Void> {

    private final String netSerial;

    public SelfTaskFunction(String netSerial) {
        this.netSerial = netSerial;
    }


    public SelfTaskFunction(String netSerial, String message) {
        this.netSerial = netSerial;
        setMessage(message);
    }


    @Override
    public void setStatus(String s) {

    }

    @Override
    public void doApply(CommStatus status, Object data) {
        log.info("{} SelfTaskFunction daApply {} {}", netSerial, status, JSONObject.toJSONString(data));
        if (CommStatus.OK == status && data instanceof List) {
            List<Object> objects = (List<Object>) data;
            for (Object obj : objects) {
                if (obj instanceof TerminalVersionInfo) {
                    versionInfo((TerminalVersionInfo) obj);
                }
                if (obj instanceof ExtendData) {
                    iccidData((ExtendData) obj);
                }
                if (obj instanceof TransForwardReply) {
                    TransForwardReply msg = (TransForwardReply) obj;
                    byte[] bytes = (byte[]) msg.getContent();
                    if (bytes[0] == 0x68 && bytes[5] == 0x68
                            && bytes[bytes.length - 1] == 0x16) {
                        for (int i = 35; i <= 55; i++) {
                            bytes[i] = (byte) (bytes[i] - 0x33);
                        }
                        electricMeter((float) (ByteUtil.bcdToInt(bytes, 35, 4, false) * 0.01));
                    }
                }
                if (obj instanceof ElectricityMeterData) {
                    ElectricityMeterData meterData = (ElectricityMeterData) obj;
                    electricMeter((float) meterData.getTotal());
                }
            }
        }
    }

    private void versionInfo(TerminalVersionInfo versionInfo) {
        ConcenReportData reportData = new ConcenReportData();
        reportData.setDate(new Date());
        reportData.setVer(versionInfo.getTerminalSoftVsn());
        BaseContainer.netStatusCallback().rccNetEquReport(netSerial, reportData);
        log.info("内部读设备版本: {} {}", versionInfo.getEquSerial(), versionInfo.getTerminalSoftVsn());
    }

    private void iccidData(ExtendData extendData) {
        ConcenReportData reportData = new ConcenReportData();
        reportData.setDate(new Date());
        if (extendData != null && extendData.getCode() == 4) {
            reportData.setIccid(extendData.getIccid());
            BaseContainer.netStatusCallback().rccNetEquReport(netSerial, reportData);
            log.info("内部读设备Iccid: {} {}", netSerial, extendData.getIccid());
        }
    }

    private void electricMeter(float electric) {
        ConcenReportData reportData = new ConcenReportData();
        reportData.setDate(new Date());
        reportData.setElectric(electric);
        BaseContainer.netStatusCallback().rccNetEquReport(netSerial, reportData);
        log.info("内部读设备电表: {} {}", netSerial, reportData.getElectric());
    }

    @Override
    public Void waitResult() {
        return null;
    }

    @Override
    public String getSerial() {
        return null;
    }

    @Override
    public int getPriority() {
        return 0;
    }
}
